<?php

namespace App\Admin\Controllers\Wechat;

use App\Admin\Controllers\BaseAdminController;
use App\Models\Common\Media;
use App\Models\Open\WechatKfMedia;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;

class WechatKfMediaController extends BaseAdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WechatKfMedia(), function (Grid $grid) {
            $grid->enableDialogCreate();
            $grid->disableEditButton();
            $grid->showQuickEditButton('编辑');

            $grid->column('typeid')->using(trans('wechat-kf-media.options.types'));
            $grid->column('title');
            $grid->column('content');
            $grid->column('description');
            $grid->column('url');
            $grid->column('pic_id')->display(function ($pic_id) {
                return $pic_id ? Media::where('id', $pic_id)->pluck('url')->toArray() : [];
            })->image('', 50);
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WechatKfMedia(), function (Show $show) {
            $show->field('id');
            $show->field('typeid');
            $show->field('content');
            $show->field('title');
            $show->field('description');
            $show->field('url');
            $show->field('pic_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WechatKfMedia(), function (Form $form) {
            $form->text('title');
            $form->select('typeid')->options(trans('wechat-kf-media.options.types'))->when(1, function (Form $form) {
                $form->textarea('content');
            })->when(2, function (Form $form) {
                $form->textarea('description');
                $form->text('url');
                $form->uploadimagepro('pic_id');
            });
        });
    }
}
